package cn.imvc.app.controllers;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.druid.util.StringUtils;

import cn.imvc.app.domains.Constants;
import cn.imvc.app.domains.EditMobileOrEmail;
import cn.imvc.app.entities.Account;
import cn.imvc.app.entities.Member;
import cn.imvc.app.models.AccountModel;
import cn.imvc.app.models.MemberModel;
import cn.imvc.app.models.SettingModel;
import cn.imvc.core.ICache;
import cn.imvc.core.Render;
import cn.imvc.util.SMSVerification;
import cn.imvc.util.ToolUtils;

@Controller
@RequestMapping("/setting")
public class SettingControllerImpl implements SettingController {

	@Autowired
	MemberModel memberModel;

	@Autowired
	SettingModel settingModel;
	
	@Autowired
	AccountModel accountModel;

	@Autowired
	ICache cache;

	@Autowired
	SMSVerification smsVerification;

	@RequestMapping("/setting.html")
	public String setting(Model model, String token) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			model.addAttribute("member", member);
			return "setting/setting";
		} else {
			return "redirect:/member/login.html";
		}
	}

	@RequestMapping("/editpw.html")
	public String editpw(String token, Model model) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			model.addAttribute("key1", member.getKey1());
			return "setting/editpw";
		} else {
			return "redirect:/member/login.html";
		}
	}

	@RequestMapping("/editpwsubmit.html")
	@ResponseBody
	public String editpwsubmit(String token, String oldpw, String newpw, String key1) {
		Map<String, String> map = settingModel.updatePw(token, oldpw, newpw, key1);
		return Render.json(map);
	}

	@RequestMapping("/editpaypw.html")
	public String editpaypw(String token, Model model) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			member = memberModel.get(member.getId());
			model.addAttribute("loginname", member.getLoginname());
			model.addAttribute("key1", member.getAccount().getKey1());
			return "setting/editpaypw";
		} else {
			return "redirect:/member/login.html";
		}
	}

	@RequestMapping("/editpaypwsubmit.html")
	@ResponseBody
	public String editpaypwsubmit(String token, String loginname, String messagecode, String paypw, String key1) {
		Map<String, String> map = settingModel.updatePayPw(loginname, messagecode, token, paypw, key1);
		return Render.json(map);
	}

	@RequestMapping("/editname.html")
	@ResponseBody
	public String editname(String token, String name) {
		Map<String, String> map = settingModel.updateName(name, token);
		return Render.json(map);
	}

	@RequestMapping("/editsex.html")
	@ResponseBody
	public String editsex(String token, int sex) {
		Map<String, String> map = settingModel.updateSex(sex, token);
		return Render.json(map);
	}

	@RequestMapping("/about.html")
	public String about(Model model, String token, HttpServletRequest request) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			String ua = request.getHeader("user-agent").toLowerCase();
			if (ua.indexOf("micromessenger") > 0) {
				model.addAttribute("isWeChat", true);
			} else {
				model.addAttribute("isWeChat", false);
			}
			return "setting/about";
		} else {
			return "redirect:/member/login.html";
		}
	}

	@RequestMapping("/on.html")
	public String on(Model model, String token) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			return "setting/on";
		} else {
			return "redirect:/member/login.html";
		}
	}

	@RequestMapping("/model.html")
	public String model(Model model, String token) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			return "setting/model";
		} else {
			return "redirect:/member/login.html";
		}
	}

	@RequestMapping("/choose.html")
	public String choose(Model model, String token, String type) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			model.addAttribute("type", type);
			if (StringUtils.isEmpty(member.getEmail())) {
				model.addAttribute("code", "0");
			} else {
				model.addAttribute("code", "1");
			}
			return "setting/choose";
		} else {
			return "redirect:/member/login.html";
		}
	}

	@RequestMapping("/verify.html")
	public String verify(Model model, String token, String verify, String type, HttpServletRequest request) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			model.addAttribute("type", type);
			model.addAttribute("verify", verify);
			if ("0".equals(verify)) {
				model.addAttribute("loginname", member.getMobile());
			} else if ("1".equals(verify)) {
				model.addAttribute("loginname", member.getEmail());
			}
			String ip = ToolUtils.getIpAddress(request);
			Integer ipTimes = cache.get(Constants.CACHENAME_MEMBER, "IP_TIMES_" + ip);
			if(ipTimes != null && ipTimes >= smsVerification.getIpTimes()){
				model.addAttribute("showImageCode", true);
			} else {
				model.addAttribute("showImageCode", false);
			}
			return "setting/verify";
		} else {
			return "redirect:/member/login.html";
		}
	}

	@RequestMapping("/editmobileoremail.html")
	@ResponseBody
	public String editmobileoremail(String token, EditMobileOrEmail entity) {
		Map<String, String> map = settingModel.updateMobileOrEmail(token, entity);
		return Render.json(map);
	}

	@RequestMapping("/updateAvatar.html")
	@ResponseBody
	public String updateAvatar(Model model, String token, MultipartFile photo) {
		Map<String, String> result = settingModel.updateAvatar(token, photo);
		return Render.json(result);
	}

	@RequestMapping("/updateAvatar.htm")
	@ResponseBody
	public String pcupdateAvatar(Model model, String token, MultipartFile file) {
		Map<String, String> result = settingModel.updateAvatar(token, file);
		return Render.json(result);
	}

	@RequestMapping("/account.htm")
	public String pcaccount(Model model, String token) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			member = memberModel.get(member.getId());
			model.addAttribute("member", member);
			Account account = accountModel.get(member.getAccount().getId());
			if(StringUtils.isEmpty(account.getPaypwd())){
				model.addAttribute("hasPayPw", false);
			} else {
				model.addAttribute("hasPayPw", true);
			}
			return "setting/pcaccount";
		} else {
			return "redirect:/member/login.htm";
		}
	}

	@RequestMapping("/editpw.htm")
	public String pceditpw(String token, Model model) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			model.addAttribute("key1", member.getKey1());
			return "setting/pceditpw";
		} else {
			return "redirect:/member/login.htm";
		}
	}

	@RequestMapping("/editpaypw.htm")
	public String pceditpaypw(String token, Model model) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			member = memberModel.get(member.getId());
			model.addAttribute("loginname", member.getLoginname());
			Account account = accountModel.get(member.getAccount().getId());
			model.addAttribute("key1", account.getKey1());
			return "setting/pceditpaypw";
		} else {
			return "redirect:/member/login.htm";
		}
	}

	@RequestMapping("/verify.htm")
	public String pcverify(Model model, String token, String verify, String type, HttpServletRequest request) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			model.addAttribute("verify", verify);
			model.addAttribute("type", type);
			if ("0".equals(verify)) {
				model.addAttribute("loginname", member.getMobile());
			} else if ("1".equals(verify)) {
				model.addAttribute("loginname", member.getEmail());
			}
			String email = member.getEmail();
			if(StringUtils.isEmpty(email)){
				model.addAttribute("ableEmail", false);
			} else {
				model.addAttribute("ableEmail", true);
			}
			String ip = ToolUtils.getIpAddress(request);
			Integer ipTimes = cache.get(Constants.CACHENAME_MEMBER, "IP_TIMES_" + ip);
			if(ipTimes != null && ipTimes >= smsVerification.getIpTimes()){
				model.addAttribute("showImageCode", true);
			} else {
				model.addAttribute("showImageCode", false);
			}
			return "setting/pcverify";
		} else {
			return "redirect:/member/login.htm";
		}
	}

	@RequestMapping("/logout.html")
	@ResponseBody
	public void logout(String token) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			memberModel.removeLoginMember(token);
		}
	}
}
